﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using ModuleDALCore;

namespace DAL
{
	public class HOSODTDAO:DALBase
	{
		public HOSODTDAO()
		{
			this.OpenConnect();
		}
		private DataTable dt;
		private string _SpName;

		public string SpName
		{
			get { return "SPHOSODT"; }
			set { _SpName = value; }
		}
		public String CreatenewId(HOSODT hs)
		{
			try
			{
				return DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(hs, null), ConvertClassToArray.GetValues(hs, 0)).Rows[0]["MABN"].ToString();

			}
			catch { return null; }//throw new InvalidExpressionException("Msg1: Can't Create new BC"); }
		}
		public bool ISEXISTSKB(HOSODT hs)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQuery(SpName, ConvertClassToArray.GetParamName(hs, null), ConvertClassToArray.GetValues(hs, 1)) == 1);

			}
			catch { return false; }//throw new InvalidExpressionException("Msg1: Can't Insert new BC"); }
		}
		public HOSODT GETHSTHEOBN(HOSODT hs)
		{
			HOSODT benhnhanc = new HOSODT();
			dt = new DataTable();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(hs, null), ConvertClassToArray.GetValues(hs, 2));
				benhnhanc = (HOSODT)ConvertDatatableToList.ConvertToObject<HOSODT>(dt);
			}
			catch { return null; }//throw new InvalidExpressionException("Msg1: Can't Find BN"); }
			return benhnhanc;
		}
		public HOSODT GETHSTHEOBNS(HOSODT hs)
		{
			HOSODT benhnhanc = new HOSODT();
			dt = new DataTable();
			try
			{
				dt = DBManager.Intance.ExecuteDataTableWithTranSaction(SpName, ConvertClassToArray.GetParamName(hs, null), ConvertClassToArray.GetValues(hs, 2));
				benhnhanc = (HOSODT)ConvertDatatableToList.ConvertToObject<HOSODT>(dt);
			}
			catch { return null; }//throw new InvalidExpressionException("Msg1: Can't Find BN"); }
			return benhnhanc;
		}
		public List<HOSODT> FindtheoIDBN(HOSODT bm)
		{
			dt = new DataTable();
			List<HOSODT> lb = new List<HOSODT>();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(bm, null), ConvertClassToArray.GetValues(bm, 3));
				lb = (List<HOSODT>)ConvertDatatableToList.ConvertTo<HOSODT>(dt);
			}
			catch
			{
				return null;
			}
			return lb;
		}
		public void BEGINTRANSACTION()
		{
			DBManager.Intance.BeginTransaction();
		}
		public void CLOSE()
		{
			DBManager.Intance.CloseConnection();
		}
		public void COMMIT()
		{
			DBManager.Intance.CommitTransaction();
		}
		public void ROLLBACK()
		{
			DBManager.Intance.RollbackTransaction();
		}
#region INSERT
		public long INSERTHS(HOSODT hs)
		{
			long idhs = 0;
			try
			{
				HOSODT hsmoi = this.GETHSTHEOBNS(hs);
				if (hsmoi != null)// ton tai
				{
					idhs = hsmoi.IDSOKHAMBENH;
				}
				else //ko ton tai
				{ 
					//insert 
					idhs = long.Parse(DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(hs, null), ConvertClassToArray.GetValues(hs, 4)).ToString());
				}
				return idhs;
			}
			catch { return 0; }
		}
		public long INSERTDK(DOTKHAM dk)
		{
			try
			{
				return long.Parse(DBManager.Intance.ExecuteNonQueryWithTransaction("SPDOTKHAM", ConvertClassToArray.GetParamName(dk, null), ConvertClassToArray.GetValues(dk, 1)).ToString());
			}
			catch { return 0; }
		}
		public bool INSERTKB(KHAMBENH kb)
		{			
			try
            {
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPKHAMBENH", ConvertClassToArray.GetParamName(kb, null), ConvertClassToArray.GetValues(kb, 1)) == 1);

            }
			catch { return false; }// throw new InvalidExpressionException("Msg1: Can't Insert new BC"); }
		}
		public bool INSERT(HOSODT hs, DOTKHAM dk, List<KHAMBENH> ds)
		{
			bool kq = true;
			long idhskb = 0;
			long iddk = 0;
			
			try
			{
				BEGINTRANSACTION();
				// INSERT ho so dieu tri truoc tien
				idhskb = INSERTHS(hs);
				if (idhskb > 0)
				{
					dk.IDHSDT = idhskb;
					//insert dot kham
					iddk = INSERTDK(dk);
					if (iddk > 0)
					{
						foreach (KHAMBENH kb in ds)
						{
							kb.IDDOTKHAM = iddk;
							//insert kham benh
							if (!INSERTKB(kb))
							{
								kq = false;
								break;
							}
						}
					}
					else
					{
						kq = false;
					}
				}
				else
				{
					kq = false;
				}
				if (kq)
				{
					COMMIT();
				}
				else
				{
					ROLLBACK();
				}
				
			}
			catch 
			{
				ROLLBACK();
				kq= false; 
			}
			this.CLOSE();
			return kq;
		}
#endregion

	}
}
